package ar.edu.unrn.lia.bean;

import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import org.primefaces.component.api.UIColumn;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;

import com.sun.faces.context.flash.ELFlash;

public class GenericBean<T> {
	

	public final static String REDIRECT_CREATE = "create?faces-redirect=true";
	public final static String REDIRECT_SEARCH = "search?faces-redirect=true";
	public final static String EMAIL_FROM = "balda@gmail.com";
	public final static  String MESSAGE_OK = "MESSAGE_OK";
	
	private LazyDataModel<T> modelLazy;

	private T entity;
	
	private Long id;	
	
	private List<SortMeta> sortDefault;	
	

	public List<SortMeta> getSortDefault() {
		return sortDefault;
	}

	public void setSortDefault(List<SortMeta> sortDefault) {
		this.sortDefault = sortDefault;
	}
	
	
	
	
	public LazyDataModel<T> getModelLazy() {
		return modelLazy;
	}


	public void setModelLazy(LazyDataModel<T> modelLazy) {
		this.modelLazy = modelLazy;
	}


	public T getEntity() {
		return entity;
	}


	public void setEntity(T entity) {
		this.entity = entity;
	}

	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public void vermensaje() {
		if (!FacesContext.getCurrentInstance().isPostback()) {
			if(ELFlash.getFlash().get(UtilsBean.MESSAGE_OK)!=null)
		     { 	 
		    	 FacesContext.getCurrentInstance().addMessage(null,	(FacesMessage) ELFlash.getFlash().get(UtilsBean.MESSAGE_OK));
		    	 ELFlash.getFlash().remove(ELFlash.getFlash().get(UtilsBean.MESSAGE_OK));	
		     }
		}
	}
	
	public void mensajeFlash(String titulo, String mensaje)
	{
		ELFlash.getFlash().put(MESSAGE_OK, new FacesMessage(FacesMessage.SEVERITY_INFO,titulo,mensaje));
		
	}	
	
	public void agregarMensaje(Severity tipo, String titulo,String mensaje) {
		FacesMessage msg = new FacesMessage(tipo,titulo,mensaje);
		FacesContext.getCurrentInstance().addMessage(null, msg);

	}
	
	public LazyDataModel<T> getList() {
		return getModelLazy();
	}
	
	public String  messagesProperty(String nombre)
	{
		return ResourceBundle.getBundle(
				"messages",
				FacesContext.getCurrentInstance().getViewRoot()
						.getLocale()).getString(nombre);
	}
	
	public SortMeta defaultFiltro(String idColumn, String campo,SortOrder order )
	{
		 UIViewRoot viewRoot =  FacesContext.getCurrentInstance().getViewRoot();
		 
		 SortMeta filtro = new SortMeta();
		 filtro.setSortBy((UIColumn)viewRoot.findComponent(idColumn));
		 filtro.setSortField(campo);
		 filtro.setSortOrder(order);
		return filtro;
	}
	
	//TODO FilterEvent e  not Working
	//Muestra los campos que estan filtrados actualmente en la grilla
	public void filterListener(String idTable) {
		 UIViewRoot viewRoot =  FacesContext.getCurrentInstance().getViewRoot();
		
		Iterator entries = ((DataTable) viewRoot.findComponent(idTable)).getFilters().entrySet().iterator();
		while (entries.hasNext()) {
		  Entry<String, String> filtro = (Entry) entries.next();
		  agregarMensaje(FacesMessage.SEVERITY_INFO, "Filtro: " + messagesProperty(filtro.getKey()), ": " + filtro.getValue());
		}
	
    }
	
}
